// ignore_for_file: depend_on_referenced_packages

import 'package:flutter_2b_client/model/IntIdAndName.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';

import '../utils/Page.dart';
import 'Initial.dart';

part 'MarketGoodsApi.g.dart';
part 'MarketGoodsApi.freezed.dart';

@RestApi()
abstract class MarketGoodsApi {

  factory MarketGoodsApi(Dio dio, {String baseUrl}) = _MarketGoodsApi;

  @GET("markets/{marketId}/goods")
  Future<Page<QueryMarketGoodsResponse>> queryMarketGoods(@Path() marketId, @Queries() QueryMarketGoodsRequest request,@Queries() Pageable pageable);

  @GET("markets/{marketId}/goods/categories")
  Future<List<IntIdAndName>> getAllGoodsCategories(@Path() marketId);

  @GET("markets/{marketId}/goods/factories")
  Future<List<IntIdAndName>> getAllGoodsFactories(@Path() marketId);

  @GET("markets/{marketId}/goods/brands")
  Future<List<IntIdAndName>> getAllGoodsBrands(@Path() marketId);

}

@freezed
class QueryMarketGoodsRequest with _$QueryMarketGoodsRequest{

  const factory QueryMarketGoodsRequest({
    String? nameOrBarcode,
    @Default([])
    List<int> brandIds,
    @Default([])
    List<int> factoryIds,
    @Default([])
    List<int> categoryIds,
  }) = _QueryMarketGoodsRequest;

  const QueryMarketGoodsRequest._();

  factory QueryMarketGoodsRequest.fromJson(Map<String, dynamic> json) => _$QueryMarketGoodsRequestFromJson(json);

  get hasAnyFilter => nameOrBarcode!= null || brandIds.isNotEmpty || factoryIds.isNotEmpty || categoryIds.isNotEmpty;

}

@freezed
class QueryMarketGoodsResponse with _$QueryMarketGoodsResponse{

  const factory QueryMarketGoodsResponse({
    required int amountInMarket,
    required int amount,
    required int id,
    required String name,
    required String barcode,
    required double price,
  }) = _QueryMarketGoodsResponse;

  const QueryMarketGoodsResponse._();

  factory QueryMarketGoodsResponse.fromJson(Map<String, dynamic> json) => _$QueryMarketGoodsResponseFromJson(json);

}

late MarketGoodsApi marketGoodsApi = MarketGoodsApi(dio, baseUrl: baseUrl);